/*
Copyright (c) 2009-2010, Warwick Warp Limited
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Warwick Warp Limited nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL WARWICK WARP LIMITED BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "options.h"

#include <iostream>
#include <iomanip>

void woptions::detail::UsageFormatter::undecorated_argument(std::string label)
{
	argument_s a;
	a.label = label;
	
	m_arguments.push_back(a);
}

void woptions::detail::UsageFormatter::argument(std::string label, size_t min_count, size_t max_count)
{
	// Required argument
	std::ostringstream to;
	if(min_count == 1 && max_count == 1)
	{
		to << "(" << label << ")";
	}
	// Optional argument
	else if(min_count == 0 && max_count == 1)
	{
		to << "[" << label << "]";
	}
	// 0 or more
	else if(min_count == 0 && max_count == std::numeric_limits<size_t>::max())
	{
		to << "(" << label << ")*";
	}
	// 1 or more
	else if(min_count == 1 && max_count == std::numeric_limits<size_t>::max())
	{
		to << "(" << label << ")+";
	}
	// n or more
	else if(max_count == std::numeric_limits<size_t>::max())
	{
		to << "(" << label << ")[" << min_count << "+]";
	}
	// Some other combination
	else if(max_count > 0)
	{
		to << "(" << label << ")[" << min_count << "-" << max_count << "]" ;
	}
	
	argument_s a;
	a.label = to.str();
	
	m_arguments.push_back(a);
}

void woptions::detail::UsageFormatter::option(std::string label, std::string args, std::string desc, std::string def_value)
{
	option_s o;
	
	o.preamble = label;
	if(!args.empty())
	{
		o.preamble += " ";
		o.preamble += args;
	}
	
	o.desc     = desc;
	if(!def_value.empty())
	{
		if(!o.desc.empty())
			o.desc += " ";
		o.desc += "[" + def_value + "]";
	}
	
	if(o.preamble.length() > m_longest_preamble)
		m_longest_preamble = o.preamble.length();
	
	m_options.push_back(o);
}

std::ostream& woptions::detail::operator<<(std::ostream& to, const UsageFormatter& fmt)
{
	size_t omax = fmt.m_longest_preamble+1;
	if(omax>30)
		omax = 30;
	
	if(fmt.m_arguments.size() > 0)
	{
		for( std::vector<UsageFormatter::argument_s>::const_iterator it=fmt.m_arguments.begin();
			 it != fmt.m_arguments.end();
			 ++it )
		{
			if(it != fmt.m_arguments.begin())
				to << " ";
			to << it->label;
		}
		to << std::endl;
	}
	
	for(std::vector<UsageFormatter::option_s>::const_iterator it=fmt.m_options.begin();
		it!=fmt.m_options.end();
		++it)
	{
		std::string fmtstring = it->preamble+" ";
		to << std::setw(omax) << std::left << fmtstring;
		
		std::string desc = it->desc;
		
		if(desc.length() == 0)
			to << std::endl;
		else
		{
			if(fmtstring.length() > omax)
				to << std::endl << std::setw(omax) << "";
			
			while(desc.length() > 79-omax)
			{
				size_t break_pos = desc.rfind(' ',79-omax);
				if(break_pos == std::string::npos)
				{
					break_pos = 79-omax;
					to << desc.substr(0,break_pos) << std::endl;
					desc = desc.substr(break_pos);
				}
				else
				{
					to << desc.substr(0,break_pos) << std::endl;
					desc = desc.substr(break_pos+1);
				}
				to << std::setw(omax) << "";
			
			}
			to << desc << std::endl;
		}
	}
	
	return to;
}

